<?php

include_once ("TaskHubDB.class.php");
include_once ("Account.class.php");


class Contact {
	/*
        public bool AddContact(uint MyAccountID, uint ContactAccountID)
        public bool DeleteContact(uint MyAccountID, uint ContactAccountID)
        public ContactList[] GetContacts(uint MyAccountID)
        public ContactList[] UpdateContacts(uint MyAccountID, String[] TelephoneNumbers)
	*/

	//////////////////////////////////////////////////////////////////////////////////////////////////
	//  Public WS methods

	public function AddContact ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_AddContact (
			$params["MyAccountID"], $params["ContactAccountID"]
		));
	}

	public function DeleteContact ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_DeleteContact (
			$params["MyAccountID"], $params["ContactAccountID"]
		));
	}

	public function GetContacts ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_GetContacts (
			$params["MyAccountID"]
		));
	}

	public function UpdateContacts ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_UpdateContacts (
			$params["MyAccountID"], $param["TelephoneNumbers"]
		));
	}


	//////////////////////////////////////////////////////////////////////////////////////////////////
	//  Private raw methods

	private function _AddContact ($MyAccountID, $ContactAccountID) {
		// Insert new Contact //
		$result = mysql_query ("INSERT INTO Contacts (AccountID,ContactAccountID) VALUES ('{$MyAccountID}', '{$ContactAccountID}')");
		if (! $result) {
			TaskHubDB::AddError (mysql_error ());
			return false;
		}

		return true;
	}

	private function _DeleteContact ($MyAccountID, $ContactAccountID) {
		// Delete Contact //
		$result = mysql_query ("DELETE FROM Contacts WHERE AccountID = '{$MyAccountID}' AND ContactAccountID = '{$ContactAccountID}'");
		if (! $result) {
			TaskHubDB::AddError (mysql_error ());
			return false;
		}

		return true;
	}

	private function _GetContacts ($MyAccountID) {
		$query = "SELECT Accounts.AccountID,Accounts.Name FROM Contacts " .
				 "INNER JOIN Accounts ON Accounts.AccountID = Contacts.ContactAccountID " .
				 "WHERE Contacts.AccountID = '{$MyAccountID}'";
		$result = mysql_query ($query);
		if (! $result) {
			TaskHubDB::AddError (mysql_error ());
			return null;
		}

		$return_array = Array ();
		while ($row = mysql_fetch_assoc ($result)) {
			$return_array[] = Array (
				"ContactID" => $row["AccountID"],
				"Name" => $row["Name"]
			);
		}

		return $return_array;
	}

    private function _UpdateContacts ($MyAccountID, $TelephoneNumbers) {
		$account = new Account ();

		// Look for accounts with those phone numbers, and add them! //
		foreach ($TelephoneNumbers as $row) {
			$contact_id = $account->_GetAccountIDByTelephoneNumber ($row["string"]);
			if ($contact_id != null) {
				$this->_AddContact ($MyAccountID, $contact_id);
			}
		}

		// Return the new Contacts //
		return $this->_GetContacts ($MyAccountID);
	}
}

?>